﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Drawing;
//using ARTemplate;
using System.Threading;
using System.Text;
using ZXing.Common;
using ZXing;
using Tools;
//using ScanTemplate.FormYJ;

namespace ScanTemplate
{
    //public delegate void DelegateShowScanMsg(Paper paper);
    public delegate void DelegateShowMsg(string msg);
    public delegate void DelegateSaveScanData(string data);
    public class ScanConfig
    {
        private UnScans _unscans;
        //private Templates _commontemplates;
        private ScanDatas _scandatas;
        private ZXing.BarcodeReader _br;
        public ScanConfig(string workpath,string outpath) //E:\Scan\s1025
        {
            Baseconfig = new BaseConfig(workpath,outpath);
            _unscans = new UnScans(Baseconfig.UnScanPath);
            //_commontemplates = new Templates(Baseconfig.TemplatePath);
            _scandatas = new ScanDatas(Baseconfig.ScanDataPath);

            //for 二维码
            DecodingOptions decodeOption = new DecodingOptions();
            decodeOption.PossibleFormats = new List<BarcodeFormat>() {
				BarcodeFormat.All_1D
			};
            _br = new BarcodeReader();
            _br.Options = decodeOption;
            //studentbase
            //Studentbases = global.GetStudentBase();
        }
        public UnScans Unscans { get { return _unscans; } }
        //public Templates CommonTemplates { get { return _commontemplates; } }
        public ScanDatas Scandatas { get { return _scandatas; } }
        //public TemplateShow Templateshow { get; set; }   
        public BaseConfig Baseconfig { get; set; }
        public ZXing.BarcodeReader BR { get { return _br; } }
        //public FormYJ.StudentBases Studentbases { get; set; }
        //
        //public ExamConfig Examconfig { get { return global.Examconfig; } }
    }
    public class BaseConfig
    {
        private string _workpath;  //E:\JHScanPaper
        private string _outpath;   //E:\JHScanPaper\out
        public BaseConfig(string workpath, string outpath) //  
        {
            this._workpath = workpath;
            this._outpath = outpath;
        }

        //public string CorrectImgPath { get { return _workpath + "\\Data\\Correct\\Scaned"; } }
        public string ExamPath { get { return _workpath + "\\Data\\Exam" ; } }
        public string ExportResultFxPath { get { return _outpath + "\\FX"; } }
        public string ExportResultPath { get { return _outpath + "\\FX"; } }
        public string ExportImageRootPath { get { return _outpath; } }

        public string ScanDataPath { get { return _workpath + "\\Data\\Scaned"; } }
        public string StudentListPath { get { return _workpath + "\\Data\\StudentList"; } }
        public string TemplatePath { get { return _workpath + "\\Data\\Template"; } }
        public string UnScanPath { get { return _workpath + "\\UnScan"; } }
        public string Workpath { get { return _workpath; } }
        public string BinDataPath { get { return _workpath + "\\Data\\BinData"; } }
        public string LogPath { get { return _workpath + "\\log"; } }
    }
   
    public class UnScans
    {
        private string _path;
        private List<UnScan> _unscans;

        public UnScans(string path)
        {
            this._path = path;
            _unscans = new List<UnScan>();
            foreach (string dirname in Tools.FileTools.SubDirNameListFromDir(path))
                Unscans.Add(new UnScan(dirname, path));
        }
        public List<UnScan> Unscans { get { return _unscans; } }
    }    
    public class UnScan
    {
        public UnScan(string dirname, string path)
        {
            this.dirname = dirname;
            this.path = path;
        }
        public FileDir Filedir(string ext=".tif")
        {
            FileDir fd = new FileDir(Fullpath, ext);
            fd.ConstructSubListFile();
            return fd;
        }        
        public bool ExistImgList()
        {
            return Tools.FileTools.ExistImageFileInDir(Fullpath );
        }
        public override string ToString()
        {
            return dirname;
        }
        public string DirName { get { return dirname; } }
        public string Fullpath { get { return path + "\\" + dirname; } }
        public string Path { get { return path; } }
        private string dirname;
        private string path;

    }
    public class ABImageFiles
    {
        private string Fullpath;
        private List<string> namelist;
        private string ext;
        public Boolean ABPage = global.ABPage;
        public Boolean A = true;
        public ABImageFiles(string Fullpath, List<string> namelist, string ext)
        {
            // TODO: Complete member initialization
            this.Fullpath = Fullpath;
            this.namelist = namelist;
            this.ext = ext;
        }
        
    }
    public class ScanDatas
    {
        private List<ScanData> _scandatas;
        private string path;
        public ScanDatas(string path)
        {
            this.path = path;
            _scandatas = new List<ScanData>();
            foreach (string dirname in Tools.FileTools.SubDirNameListFromDir(path))
            {
                Scandatas.Add(new ScanData(dirname, path));
            }
        }
        public List<ScanData> Scandatas { get { return _scandatas; } }
    }
    public class ScanData
    {
        public ScanData(string dirname, string path)
        {
            this._dirname = dirname;
            this._path = path;
            this._templatename = path + "\\" + dirname + "\\template.json";
           
            _examname = dirname;           
            string sinfofilename = _path+"\\"+dirname + "\\config.json";
            _Sinfo = null;
            if (File.Exists(sinfofilename))
            {
                this._Sinfo = ScannedInfo.Load(sinfofilename);
                _examname = _Sinfo.ExamName;
            }            
        }
        public FileDir Filedir(string ext = ".tif")
        {
            FileDir fd = new FileDir(Imgpath, ext);
            fd.ConstructSubListFile();
            return fd;
        }

        public override string ToString()
        {
            if (File.Exists(Fullpath + "\\已生成阅卷数据.txt"))
                return _examname + " 已生成阅卷数据";
            return _examname;
        }      
        public string TemplateFileName
        {
            get
            {
                if (!File.Exists(_templatename))
                    return "";
                return _templatename;
            }
        }
        public string Fullpath { get { return _path + "\\" + _dirname; } }
        public string Imgpath { get { return _path + "\\" + _dirname + "\\img"; } }
        public string DataFullName { get { return Fullpath + "\\data.txt.json"; } }
        public string ExamName { get { return _examname; } }
        private string _dirname;
        private string _path;
        private string _templatename;
        private string _examname;
        private ScannedInfo _Sinfo;


        //public ScanDataInfo ScanDatainfo { get; set; }

        internal ScannedInfo Sinfo()
        {
            return _Sinfo;
        }
    }    
    public class ValueTag
    {
        public ValueTag(string value, Object tag)
        {
            this.Value = value;
            this.Tag = tag;
        }
        public Object Tag;
        public String Value;
        public override string ToString()
        {
            return Value;
        }
    }    
}
